/* ====================================================================
 * (#)Header: Voter.java
 * (#)Revision: 
 * (#)Date Created: Mar 26, 2013
 * (#)Created By: Karol Wozniak
 * ====================================================================
 *
 * Copyright (c) 2013 karolak .  All rights reserved.
 *
 * ====================================================================
 * 
 * The software application represented here is the sole property of 
 * karolak. Its unauthorized utilization is expressly prohibited.
 * 
 * ====================================================================
 */
package com.karolak.stock.strategy;

import java.text.DecimalFormat;
import java.util.LinkedHashMap;
import java.util.Map;

import com.karolak.stock.data.DataManager;
import com.karolak.stock.indicator.Indicator;
import com.karolak.stock.oscilator.Oscilator;

/**
 * <code>Voter</code> is used to ....
 * 
 * @author <a href="mailto:kwozniak@hotmail.com">Karol Wozniak</a>
 */
public class Voter extends Oscilator {

	private Indicator[] oscilators;
	private double signalThreshold;
	private double votingThreshold;

	private DecimalFormat format;

	public Voter(String name, DataManager dataManager, int period, double signalThreshold, double votingThreshold, Indicator... oscilators) {
		super(dataManager, period, null);
		this.signalThreshold = signalThreshold;
		this.votingThreshold = votingThreshold;
		this.oscilators = oscilators;
		this.format = new DecimalFormat("0.0000");
		setName(name);
	}

	public double execute(double x) {
		double totLong = 0;
		double totShort = 0;
		double totZero = 0;
		double retValue = 0;
		for (Indicator osc : getOscilators()) {
			double v = getDataManager().getLast(osc.getName());
			if (v >= getSignalThreshold()) {
				totLong++;
			} else if (v <= -getSignalThreshold()) {
				totShort++;
			} else if (v == 0) {
				totZero++;
			}
			// System.out.println(String.format("Indicator: %s, vote: %s", str,
			// v));
		}
		// System.out.println(String.format("Vote: long: %s, short: %s, zero: %s, total : %s",
		// totLong, totShort, totZero, getInputSeries().length));
		retValue = totLong > totShort ? totLong : -totShort;
		retValue = retValue / getOscilators().length;
		retValue = Math.abs(retValue) >= getVotingThreshold() ? retValue : 0;
		return retValue;
	}

	public Map<String, String> getResults() {
		Map<String, String> result = new LinkedHashMap<String, String>();
		for (Indicator osc : getOscilators()) {
			double v = getDataManager().getLast(osc.getName());
			result.put(osc.getName(), getFormat().format(v));
		}
		return result;
	}

	public double getSignalThreshold() {
		return signalThreshold;
	}

	public void setSignalThreshold(double signalThreshold) {
		this.signalThreshold = signalThreshold;
	}

	public double getVotingThreshold() {
		return votingThreshold;
	}

	public void setVotingThreshold(double votingThreshold) {
		this.votingThreshold = votingThreshold;
	}

	public Indicator[] getOscilators() {
		return oscilators;
	}

	public DecimalFormat getFormat() {
		return format;
	}
}
